package com.example.superoa.model.service;

import java.math.BigDecimal;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.KqAttendanceDao;
import com.example.superoa.model.dao.KqBanciDao;
import com.example.superoa.model.dao.KqLeaveTypeDao;
import com.example.superoa.model.dao.QingjiaDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.LeaveClassMapper;
import com.example.superoa.model.mappers.LeaveMapper;
import com.example.superoa.model.pojos.KqAttendance;
import com.example.superoa.model.pojos.KqBanci;
import com.example.superoa.model.pojos.LeaveType;
import com.example.superoa.model.pojos.Qingjia;
import com.example.superoa.model.pojos.Staff;

@Service
@Transactional(rollbackFor=Exception.class)
public class LeaveService {
	@Autowired
	private LeaveMapper map;
	
	@Autowired
	private DakaService d_ser;
	
	@Autowired
	private AttendService a_ser;
	
	@Autowired
	private KqAttendanceDao a_dao;
	
	@Autowired
	private QingjiaDao q_dao;
	
	@Autowired
	private KqBanciDao b_dao;
	
	@Autowired
	private StaffDao s_dao;
	
	
	private Shijian  sj=new Shijian();
	
	//新增请假记录,判断是否有未申请原因来决定是否为补录
	public void addLeave(Qingjia leave,int tian,Integer id) {
		Staff staff=s_dao.findById(id).get();
		leave.setStaff(staff);
		leave.setLDate(new Date(sj.getNetworkTime()));
		if(leave==null&&leave.getLNowhy().equals("")) {
			leave.setLChu(1);
		}else {
			leave.setLChu(2);
		}
		if(leave.getlType()==1) {
			leave.setLTime(new BigDecimal(tian));
		}else {
			leave.setLTime(new BigDecimal(0.5));
		}
		leave.setLShen(0);
		leave.setLXshen(a_ser.selectDeptStaff(id)+"");
		
		q_dao.save(leave);
		
	}
	public Integer panduan(Date date,Integer time,Integer staff_id) {
		Date jie=sj.getDaiDate(date, time);
		JSONArray json=map.selectQingjia(staff_id);
		List<Date> list=sj.getDays(1, date, jie);
		for (Date d : list) {
			for (Object object : json) {
				JSONObject obj=(JSONObject)object;
				if(sj.belongCalendar(d, obj.getDate("L_KAI"),  obj.getDate("L_JIE"), 2)) {
					return 1;
				}
			}
		}
		return 0;
	}
	public JSONArray selectAllMonth(Date mon) {
		List<Integer> date=sj.getyearmonth(mon);
		List<Staff> list=(List<Staff>) s_dao.findAll();
		List<String> list1;
		if(sj.booleanMonth(mon)) {
			list1=sj.getDayByMonth(date.get(0), date.get(1)-1, date.get(2),2);
		}else {
			list1=sj.getDayByMonth(date.get(0), date.get(1)-1, 1,1);
		}
		JSONArray arr=new JSONArray();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		for (Staff staff : list) {
			JSONArray qing=map.selectQingjia(staff.getStaffId());
			Integer shi=0,bing=0,xiu=0,qi=0;
			for (String string : list1) {
				Date day = null;
				try {
					day=sdf.parse(string);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				for (Object o2 : qing) {
					JSONObject objjj=(JSONObject)o2;
					if(sj.belongCalendar(day, objjj.getDate("L_KAI"), objjj.getDate("L_JIE"), 2)) {
						if(objjj.getInteger("L_CLASS")==1) {
							shi++;
						}else if(objjj.getInteger("L_CLASS")==2) {
							bing++;
						}else {
							qi++;
						}
						break;
					}
				}
			}
			JSONObject obj=new JSONObject();
			obj.put("shi", shi);
			obj.put("bing", bing);
			obj.put("xiu", xiu);
			obj.put("qi", qi);
			arr.add(obj);
		}
		return arr;
	}
	
	public JSONArray selectAll() {
		return map.LeaveSelectAll();
	}
	public Integer selectMonthCount(int id,int qclass,int year,int month) {
		return map.selectLeaveCount(id, qclass,year,month);
	}
	public Integer selectIDD(int id) {
		return map.selectIDD(id);
	}
	public JSONObject selectid(int id) {
		return map.selectId(id);
	}
	public Boolean selectbetween(int id,Date date) {
		if(map.selectbetween(id, date)>=1) {
			return true;
		}
		return false;
	}
	public JSONObject selectBqLeave(Integer id,Date date) {
		return map.selectBqLeave(id, date);
	}
	public void shen(int id,int shen,String str) {
		Qingjia qj=q_dao.findById(id).get();
		if(shen==2) {
			qj.setLBou(str);
		}
		if(qj.getLChu()==2) {
			JSONArray json=a_ser.selectSousuo(qj.getLKai(), qj.getLJie(), qj.getStaff().getStaffId());
			if(json!=null) {
				if(qj.getlType()==1) {
						for (Object object : json) {
							JSONObject obj=(JSONObject)object;
							KqAttendance atten=new KqAttendance();
							atten.setAId(obj.getInteger("a_id"));
							atten.setADao(new Time(obj.getDate("a_dao").getTime()));
							atten.setATui(new Time(obj.getDate("a_tui").getTime()));
							atten.setADate(obj.getDate("a_date"));
							atten.setStaff(s_dao.findById(obj.getInteger("staff_id")).get());
							atten.setKqBanci(b_dao.findById(obj.getInteger("b_id")).get());
						}
				}else {
					JSONObject obj=(JSONObject) json.get(0);
					System.out.println(obj);
					KqAttendance attend=a_dao.findById(obj.getInteger("a_id")).get();
					d_ser.Dao(attend.getStaff().getStaffId(), attend.getADao(), attend.getADate(), attend);
					d_ser.Tui(attend, attend.getATui());
				}
			}
		}
		qj.setlSdate(new Date(sj.getNetworkTime()));
		qj.setLShen(shen);
		q_dao.save(qj);
	}

	
}
